import 'package:flutter/material.dart';
import 'package:mybase/base/tool/color/FFColor.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/tool/eventBus/eventBus.dart';
import 'package:mybase/base/tool/extension/FFColorExt.dart';
import 'package:mybase/base/tool/extension/FFStringExt.dart';
import 'package:shared_preferences/shared_preferences.dart';

/**
 * 轻量级缓存工具
 */

class FFSpUtils {
  static SharedPreferences? _sharedPreferences;

  static initSp() async {
    _sharedPreferences ??= await SharedPreferences.getInstance();
  }

  static Future<bool> remove(String key) {
    return _sharedPreferences!.remove(key);
  }

  //有过期时间： 超过时间就会过期
  static String getDataExpired(String key) {
    if (_sharedPreferences == null) {
      return "";
    }

    var i = DateTime.now().millisecondsSinceEpoch;
    String? targeti = _sharedPreferences!.getString("$key.expiredmill") ?? "0";
    int targetSecond = targeti.toINT();
    String s = _sharedPreferences!.getString(key) ?? "";
    if (i <= targetSecond) {
      return s;
    }

    //过期了
    s = "";
    return s;
  }

  //有过期时间： second多少秒后失效
  static Future saveDataExpired(String key, String data, int mill) async {
    int value = DateTime.now().millisecondsSinceEpoch + mill;
    await _sharedPreferences!.setString("$key.expiredmill", value.toString());
    return await _sharedPreferences!.setString(key, data);
  }

  static Future<bool> saveData(String key, String? data) async {
    if (FFEmpty.stringIsEmpty(data)) {
      await remove(key);
      return false;
    }
    return await _sharedPreferences!.setString(key, data!);
  }

  static String? getData(String key) {
    if (_sharedPreferences == null) {
      return null;
    }
    return _sharedPreferences!.getString(key);
  }

  static Future saveColor(String key, Color? data) async {
    if (data == null) {
      await remove(key);
      return;
    }
    return await _sharedPreferences!.setString(key, data.toHexStr);
  }

  static Color? getColor(String key) {
    if (_sharedPreferences == null) {
      return null;
    }
    String? string = _sharedPreferences!.getString(key);
    if (FFEmpty.stringIsEmpty(string)) {
      return null;
    }
    return hex(string!);
  }

  static Future saveColorList(String key, List<Color>? list) async {
    if (list == null) {
      await remove(key);
      return;
    }
    List<String> list2 = list.map((e) => e.toHexStr).toList();
    var join = list2.join(",");

    return await _sharedPreferences!.setString(key, join);
  }

  static List<Color>? getColorList(String key) {
    if (_sharedPreferences == null) {
      return null;
    }
    String? string = _sharedPreferences!.getString(key);
    if (FFEmpty.stringIsEmpty(string)) {
      return null;
    }

    List<String>? list = string?.split(",");
    if (FFEmpty.listIsEmpty(list)) return null;
    return list!.map((e) => hex(e)).toList();
  }

  static bool getBoolData(
    String key,
  ) {
    return _sharedPreferences!.getBool(key) ?? false;
  }

  static bool? getBoolNullData(
    String key,
  ) {
    return _sharedPreferences!.getBool(key);
  }

  static Future saveBoolData(String key, bool data) async {
    return await _sharedPreferences!.setBool(key, data);
  }

  static int? getIntData(
    String key,
  ) {
    return _sharedPreferences!.getInt(key);
  }

  static Future saveIntData(String key, int data) async {
    return await _sharedPreferences!.setInt(key, data);
  }

  static double? getDoubleData(
    String key,
  ) {
    return _sharedPreferences!.getDouble(key);
  }

  static Future saveDoubleData(String key, double data) async {
    return await _sharedPreferences!.setDouble(key, data);
  }
}
